package com.basic.modules.nykf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.modules.nykf.dao.PermissionDao;
import com.basic.modules.nykf.service.PermissionService;
import com.basic.modules.sys.entity.SysUserEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, SysUserEntity> implements PermissionService {

    @Override
    public List<SysUserEntity> getUserListByRoleId(long roleId) {
        List<SysUserEntity> userListByRoleId = baseMapper.getUserListByRoleId(roleId);
        return userListByRoleId;
    }

    public static void main(String[] args) {
        List<Map> mapList = new ArrayList<>();
        Map map = new HashMap();
        map.put("kfxm", "客服1");
        map.put("ksrq", "2025-05-01");
        map.put("jsrq", "2025-05-09");

        Map map1 = new HashMap();
        map1.put("kfxm", "客服2");
        map1.put("ksrq", "2025-05-10");
        map1.put("jsrq", "2025-05-19");

        Map map2 = new HashMap();
        map2.put("kfxm", "客服3");
        map2.put("ksrq", "2025-05-20");
        map2.put("jsrq", "2025-05-25");

        Map map3 = new HashMap();
        map3.put("kfxm", "客服4");
        map3.put("ksrq", "2025-05-26");
        map3.put("jsrq", "2025-06-15");

        Map map4 = new HashMap();
        map4.put("kfxm", "客服5");
        map4.put("ksrq", "2025-06-26");
        map4.put("jsrq", "2188-01-01");

        mapList.add(map);
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);

        // 根据开始日期和结束日期计算在五月分有工期的客服,并算出来这个客服再5月份工作了几天

        // 定义五月份的起始和结束日期
        LocalDate mayStart = LocalDate.of(2025, 5, 1);
        LocalDate mayEnd = LocalDate.of(2025, 5, 31);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 用于存储每个客服在五月的工作天数
        Map<String, Long> workDaysMap = new HashMap<>();

        for (Map m : mapList) {
            String kfxm = (String) m.get("kfxm");
            LocalDate ksrq = LocalDate.parse((String) m.get("ksrq"), formatter);
            LocalDate jsrq = LocalDate.parse((String) m.get("jsrq"), formatter);

            // 计算与五月重叠的日期范围
            LocalDate overlapStart = ksrq.isBefore(mayStart) ? mayStart : ksrq;
            LocalDate overlapEnd = jsrq.isAfter(mayEnd) ? mayEnd : jsrq;

            // 如果存在重叠时间段
            if (!overlapStart.isAfter(overlapEnd)) {
                long days = ChronoUnit.DAYS.between(overlapStart, overlapEnd) + 1;
                workDaysMap.merge(kfxm, days, Long::sum);
            }
        }

        System.out.println("五月各客服工作天数:");
        workDaysMap.forEach((kfxm, days) -> {
            System.out.println(kfxm + ": " + days + "天");
        });

    }
}
